<?php
namespace common\components;

use yii;
use common\models\Sltopic;
use common\models\Zytopic;
use common\models\Zhongyao;
use yii\helpers\Url;

class FrontendHelper {

    // 根据 食材大类别名 查询 具体食材
    public static function Shicai_type($type_name) {
        if ($type_name !== null) {
            $query = Sltopic::find()->select(['id', 'title', 'img'])->where(['type' => $type_name])->asArray()->all();
        } else {
            $query = array();
        }
        return $query;
    }

    // 根据 重要形狀 查询 具体藥物
    public static function Zhongyao_type($type_name) {
        if ($type_name !== null) {
            $query = Zytopic::find()->select(['id', 'title', 'img'])->where(['type' => $type_name])->asArray()->all();
        } else {
            $query = array();
        }
        return $query;
    }

    // 隨機查詢藥物
    public static function Zhongyao_random($num=10) {
        $query = Zhongyao::find()->select(['id', 'title'])->where(['enable'=>1])->limit($num)->asArray()->all();
        return $query;
    }

    // 相关藥物类别
    public static function Zytopic_relate($id) {
        if ($id !== null) {
            $query = Zytopic::find()->select(['type' ,'title'])->where(['id' => $id])->asArray()->one();
            $relate_type = Zytopic::find()->select(['id','title'])->where(['!=','title',$query['title']])
                    ->andWhere(['type' => $query['type']])->limit(15)->asArray()->all();
        } else {
            $relate_type = array();
        }
        return $relate_type;
    }

    // 相关食材类别
    public static function Sltopic_relate($id, $limit) {
        if ($id !== null) {
            $query = Sltopic::find()->select(['type' ,'title'])->where(['id' => $id])->asArray()->one();
            $relate_type = Sltopic::find()->select(['id','title'])->where(['!=','title',$query['title']])
                    ->andWhere(['type' => $query['type']])->limit($limit)->asArray()->all();
        } else {
            $relate_type = array();
        }
        return $relate_type;
    }

    // 根据食材类别名id查询名称
    public static function Shicai_name($type_id) {
        if ($type_id !== null) {
            $query = Sltopic::find()->select('title')->where(['id' => $type_id])->asArray()->one();
            $title = $query['title'];
        } else {
            $title = '';
        }
        return $title;
    }
}
